<template>
  <div v-loading="loading" ref="chart" style="height:300px;width:100%" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: ['checkLine'],
  data() {
    return {
      chart: null,
      seriesData: [],
      loading: false,
    }
  },
  watch: {
    checkLine: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.loading = true
      if (!this.$refs.chart) {
        return;
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(
        {
          tooltip: {
            trigger: 'axis'
          },
          legend: {},
          toolbox: {
            show: true,
          },
          xAxis: {
            type: 'category',
            data: this.checkLine.map(o => o.day)
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '检测数量',
              type: 'line',
              data: this.checkLine.map(o => o.count),
              markPoint: {
                data: [
                  { type: 'max', name: 'Max' },
                  { type: 'min', name: 'Min' }
                ]
              }
            }
          ]
        }
        )
      this.loading = false
    }
  }
}
</script>
